﻿<UserControl x:Class="VSTalk.Engine.View.CommonChatFrame"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:VSTalk.Engine.ViewModel"
             xmlns:view="clr-namespace:VSTalk.Engine.View"
             xmlns:conerters="clr-namespace:VSTalk.Engine.Converters"
             xmlns:messenger="clr-namespace:VSTalk.Engine.ViewModel.Messenger"
             xmlns:controls="clr-namespace:VSTalk.Engine.Controls"
             mc:Ignorable="d"
             x:Name="ChatFrameControl">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="..\Resources\Icons.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <conerters:VisibleIfFalseConverter x:Key="VisibleIfFalseConverter" />
            <conerters:VisibleIfTrueConverter x:Key="VisibleIfTrueConverter" />

            <DataTemplate x:Key="HeaderContactTabTemplate"
                          DataType="{x:Type messenger:IChatViewModel}">
                <DockPanel>
                    <DockPanel.Resources>
                        <DataTemplate DataType="{x:Type messenger:InterlocutorChatViewModel}">
                            <controls:InterlocutorLabel Interlocutor="{Binding Interlocutor}"
                                                        FontSize="15" />
                        </DataTemplate>
                        <DataTemplate DataType="{x:Type messenger:RoomChatViewModel}">
                            <controls:RoomLabel Room="{Binding Room}"
                                                FontSize="15" />
                        </DataTemplate>
                    </DockPanel.Resources>
                    <Button DockPanel.Dock="Right"
                            Command="{Binding ElementName = ChatFrameControl, Path = DataContext.FinishConversation}"
                            CommandParameter="{Binding}"
                            Margin="5,0,0,0"
                            Style="{DynamicResource CloseTabButton}" />
                    <ContentPresenter Content="{Binding}" />
                </DockPanel>
            </DataTemplate>

            <DataTemplate DataType="{x:Type messenger:ChatFrameViewModel}">
                <Grid>
                    <Grid Visibility="{Binding HasActiveChats, Converter={StaticResource VisibleIfTrueConverter}}">
                        <Grid.InputBindings>
                            <KeyBinding Gesture="CTRL+Enter"
                                        Command="{Binding SendMessage}" />
                        </Grid.InputBindings>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="40" />
                        </Grid.RowDefinitions>
                        <TabControl Grid.Row="0"
                                    ItemsSource="{Binding ActiveChats}"
                                    ItemTemplate="{StaticResource HeaderContactTabTemplate}"
                                    SelectedItem="{Binding CurrentChat, Mode=TwoWay}" />
                        <Grid Grid.Row="1">
                            <StackPanel Orientation="Horizontal"
                                        HorizontalAlignment="Right">
                                <Button Content="Send"
                                        Command="{Binding SendMessage}"
                                        Padding="5"
                                        Focusable="False"
                                        ToolTip="CTRL+Enter" />
                            </StackPanel>
                        </Grid>
                    </Grid>

                    <!-- show contact list -->
                    <Grid Visibility="{Binding HasActiveChats, Converter={StaticResource VisibleIfFalseConverter}}">
                        <StackPanel Orientation="Vertical">
                            <Label HorizontalAlignment="Center">Please select a contact.</Label>
                            <TextBlock HorizontalAlignment="Center">
                                <Hyperlink Command="{Binding ShowMainControl}">Show contact list</Hyperlink>
                            </TextBlock>
                        </StackPanel>
                    </Grid>
                </Grid>
            </DataTemplate>

            <DataTemplate DataType="{x:Type messenger:InterlocutorChatViewModel}">
                <view:IterlocutorChat />
            </DataTemplate>

            <DataTemplate DataType="{x:Type messenger:RoomChatViewModel}">
                <view:RoomChat />
            </DataTemplate>
        </ResourceDictionary>
    </UserControl.Resources>

    <ContentPresenter Content="{Binding}" />
</UserControl>
